<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/roles' }">角色列表</el-breadcrumb-item>
      <el-breadcrumb-item>角色修改</el-breadcrumb-item>
    </el-breadcrumb>

    <el-row :gutter="20">
      <el-col :span="7">
        <div class="grid-content bg-purple">
          <el-card style="height: 550px;overflow: auto;">
            <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
              <el-tab-pane label="菜单权限" name="vue">
                <el-tree
                    :data="data"
                    :props="defaultProps"
                    node-key="menuCode"
                    show-checkbox
                    :default-expand-all="true"
                    :default-checked-keys="defkeys"
                    ref="treeRef"
                ></el-tree>
              </el-tab-pane>
              <el-tab-pane label="接口权限" name="api">
                <el-tree
                    :data="apiData"
                    :props="defaultPropsAPI"
                    node-key="id"
                    show-checkbox
                    :default-expand-all="true"
                    :default-checked-keys="defApi"
                    ref="treeAPI"
                >
                <span class="custom-tree-node" slot-scope="{ node, data }">
                  <el-tooltip
                      class="item"
                      effect="dark"
                      :content="data.purviewName?data.purviewName:'暂无注释'"
                      placement="top-start"
                      :enterable="false"
                  >
                      <span>{{ node.label }}</span>
                  </el-tooltip>
                </span>


                </el-tree>
              </el-tab-pane>
            </el-tabs>


          </el-card>
        </div>
      </el-col>
      <el-col :span="17">
        <el-card>
          <el-form ref="form" :model="role" label-width="80px">
            <el-form-item label="角色名称">
              <el-input v-model="role.roleName"></el-input>
            </el-form-item>
            <el-form-item label="创建时间">
              <el-input v-model="role.createTime" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="角色介绍">
              <el-input
                  type="textarea"
                  v-model="role.remark"
                  :autosize="{ minRows: 7, maxRows: 6 }"
              ></el-input>
            </el-form-item>
          </el-form>
        </el-card>
        <div class="but_cl">
          <el-button @click='Tes()'>取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </div>
      </el-col>
    </el-row>
  </div>
</template>
<script>

import {getByModuleCode, saveModuleCode} from '@/api/RoleApi'
import {queryRolePurview} from '@/api/MallSysPurviewApi.js'

export default {
  data() {
    return {
      data: [],
      defaultProps: {
        children: 'children',
        label: 'menuName'
      },
      defaultPropsAPI: {
        children: 'children',
        label: 'purviewUrl'
      },
      defkeys: [],
      role: {},
      activeName: "vue",
      apiData: [],
      defApi: []
    }
  },
  created() {
    this.queryPage()
    this.getRolePurview()
  },
  methods: {
    //初始化表单
    Tes() {
      this.$router.push({path: '/roles'})
    },
    handleClick(tab, event) {
      console.log(event);
      console.log(tab);
    },
    queryPage() {
      getByModuleCode(this.$route.query.jId).then(res => {
        //if (res.code != 20000) this.$message.error(res.message)
        this.data = res.data.tree
        this.defkeys = res.data.finallyList
        this.role = res.data.role
      })

    },
    getRolePurview() {
      queryRolePurview(this.$route.query.jId).then(res => {
        console.log(res)
        this.apiData = res.data.list
        this.apiData.forEach(res=>{
          if (res.isBelong===1) {
            this.defApi.push(res.id)
          }
        })
      })
    },
    async save() {
      //获取所有被选中的属性
      const keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys()
      ]
      const treeAPI = [
        ...this.$refs.treeAPI.getCheckedKeys(),
        ...this.$refs.treeAPI.getHalfCheckedKeys()
      ]
      //获取角色信息
      const roleht = this.role
      roleht.list = keys
      roleht.apiList = treeAPI
      saveModuleCode(roleht).then(res => {
        //if (res.code != 20000) this.$message.error(res.message)
        this.data = []
        this.defkeys = []
        this.role = {}
        this.$router.push({path: '/roles'})
        return this.$notify({title: '成功', message: '修改成功', type: 'success'});
      })


    }
  }
}
</script>
<style lang="less" scoped>
.el-card {
  width: 100%;
  // height: 150%;
}

.but_cl {
  margin-top: 20px;
  position: absolute;
  left: 50%;

}

</style>
